﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AIConvertTool
{
    /// <summary>
    /// AISettingsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AISettingsWindow : Window
    {
        static string iniPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.ini");
        IniFiles ini = new IniFiles(iniPath);
        public AISettingsWindow()
        {
            InitializeComponent();

            getIniFileItems();
        }

        private void getIniFileItems()
        {
            string aiType = ini.IniReadValue("AISetting", "AI_TYPE");
            switch (aiType)
            {
                case "1":
                    OpenAIRaido.IsChecked = true;
                    API_URL.Text = ini.IniReadValue("AISetting", "API_URL");
                    API_KEY.Text = ini.IniReadValue("AISetting", "API_KEY");
                    API_MODEL.Text = ini.IniReadValue("AISetting", "API_MODEL");
                    break; 
                case "2":
                    YiyanRadio.IsChecked = true;
                    API_URL.Text = ini.IniReadValue("AISetting", "YIYAN_API_URL");
                    API_KEY.Text = ini.IniReadValue("AISetting", "YIYAN_API_KEY");
                    API_MODEL.Text = ini.IniReadValue("AISetting", "YIYAN_API_MODEL");
                    break; 
                case "3":
                    QwenRadio.IsChecked = true;
                    API_URL.Text = ini.IniReadValue("AISetting", "QWEN_API_URL");
                    API_KEY.Text = ini.IniReadValue("AISetting", "QWEN_API_KEY");
                    API_MODEL.Text = ini.IniReadValue("AISetting", "QWEN_API_MODEL"); 
                    break; 
                default:
                    QwenRadio.IsChecked = true;
                    ini.IniWriteValue("AISetting", "AI_TYPE", "1");
                    API_URL.Text = ini.IniReadValue("AISetting", "API_URL");
                    API_KEY.Text = ini.IniReadValue("AISetting", "API_KEY");
                    API_MODEL.Text = ini.IniReadValue("AISetting", "API_MODEL");
                    break;
            }

            if (ini.IniReadValue("ZBlog", "USE") == "1")
            {
                ZblogSwitch.IsChecked = true;
            } else
            {
                ZblogSwitch.IsChecked = false;
            }
            ZBlogUSERNAME.Text = ini.IniReadValue("ZBlog", "USERNAME");
            ZBlogPASSWORD.Text = ini.IniReadValue("ZBlog", "PASSWORD");

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string aiType = ini.IniReadValue("AISetting", "AI_TYPE");
            switch (aiType)
            {
                case "1":
                    ini.IniWriteValue("AISetting", "API_URL", API_URL.Text);
                    ini.IniWriteValue("AISetting", "API_KEY", API_KEY.Text);
                    ini.IniWriteValue("AISetting", "API_MODEL", API_MODEL.Text);
                    break;
                case "2":
                    ini.IniWriteValue("AISetting", "YIYAN_API_URL", API_URL.Text);
                    ini.IniWriteValue("AISetting", "YIYAN_API_KEY", API_KEY.Text);
                    ini.IniWriteValue("AISetting", "YIYAN_API_MODEL", API_MODEL.Text);
                    break;
                case "3":
                    ini.IniWriteValue("AISetting", "QWEN_API_URL", API_URL.Text);
                    ini.IniWriteValue("AISetting", "QWEN_API_KEY", API_KEY.Text);
                    ini.IniWriteValue("AISetting", "QWEN_API_MODEL", API_MODEL.Text);
                    break;
                default:
                    ini.IniWriteValue("AISetting", "AI_TYPE", "1");
                    ini.IniWriteValue("AISetting", "API_URL", API_URL.Text);
                    ini.IniWriteValue("AISetting", "API_KEY", API_KEY.Text);
                    ini.IniWriteValue("AISetting", "API_MODEL", API_MODEL.Text);
                    break;
            }

            if (ZblogSwitch.IsChecked == false)
            {
                ini.IniWriteValue("ZBlog", "USE", "0");
            }
            else
            {
                if (ZBlogUSERNAME.Text == "" || ZBlogPASSWORD.Text == "")
                {
                    MessageBox.Show("请设置ZBlog账号密码。");
                    ini.IniWriteValue("ZBlog", "USE", "0");
                    return;
                }
                ini.IniWriteValue("ZBlog", "USE", "1");
            }
            ini.IniWriteValue("ZBlog", "USERNAME", ZBlogUSERNAME.Text);
            ini.IniWriteValue("ZBlog", "PASSWORD", ZBlogPASSWORD.Text);

            Close();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            ini.IniWriteValue("AISetting", "AI_TYPE", "1");
            getIniFileItems();
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            ini.IniWriteValue("AISetting", "AI_TYPE", "2");
            getIniFileItems();
        }

        private void RadioButton_Checked_2(object sender, RoutedEventArgs e)
        {
            ini.IniWriteValue("AISetting", "AI_TYPE", "3");
            getIniFileItems();
        }
    }
}
